package org.rsna.radlex;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.StringTokenizer;

import edu.stanford.smi.protege.model.Cls;
import edu.stanford.smi.protege.model.Frame;
import edu.stanford.smi.protege.model.Instance;
import edu.stanford.smi.protege.model.KnowledgeBase;
import edu.stanford.smi.protege.model.Project;


public class IDFinder {


	
	//old
	//private static final String PROJECT_FILE_NAME = "C:\\Radlex\\2010-09-17\\Radlex.pprj";
	//private static final String DATA_LOAD_FILE_NAME = "C:\\Radlex\\playbook\\run.csv";

	//new
	private static final String PROJECT_FILE_NAME = "C:\\ObsoleteOmit\\Radlex.pprj";
	private static final String DATA_LOAD_FILE_NAME = "C:\\ObsoleteOmit\\all_terms_4_beverly.csv";
	 
		
		// @todo: make sure we're not doulbe updating
		public static void main(String[] args) throws Exception   
		{
			HashSet<String> idsLoaded = new HashSet<String>();
			Collection<Cls> errors = new ArrayList<Cls>();
	        Project project = new Project(PROJECT_FILE_NAME, errors);
	        CommonTasks.displayErrors(errors);
	        KnowledgeBase kb = project.getKnowledgeBase();
	        
	        //old
	       // HashMap<String, String> synHash = CommonTasks.getAllSynonyms(kb);
	        //new
	        HashMap<String, String> synHash = CommonTasks.getAllSynonymsCUSTOM(kb);
	        

			ArrayList <ArrayList> fullFile = new ArrayList<ArrayList>();
			
			File file = new File(DATA_LOAD_FILE_NAME);
			BufferedReader bufRdr = new BufferedReader(new FileReader(file));
			String line = null;
			while( (line =  bufRdr.readLine()) != null ) {

				ArrayList <String> singleLine = new ArrayList<String>();
				singleLine.add(line);
				fullFile.add(singleLine);
			}
	        
			
			//7-8-2011 MJ - Adding in term comparison for obsolete radlex terms.
			//Obsolete term Comparison List
			ArrayList<String> obsoleteTerms = new ArrayList<String>();
			CommonTasks CT = new CommonTasks();
			//RID15849 = Obsolete Radlex term
			Cls myObsoleteCls = CT.getClassByRID("RID15849", kb);
			Collection myClsCollection = myObsoleteCls.getSubclasses();
			Iterator itr = myClsCollection.iterator();
			while(itr.hasNext())
			{
				Cls currentCls = (Cls) itr.next();
				String curTerm = CT.getNamesForClass(currentCls).toString().replace("]", "");
				curTerm = curTerm.replace("[", "");
				obsoleteTerms.add(curTerm.toLowerCase().trim());
			}
			
			
			
			
			//ArrayList<ArrayList> fullFile = FileUtils.readLinesFromFile(DATA_LOAD_FILE_NAME);
			Iterator<ArrayList> iter = fullFile.iterator();
			HashMap<String, String> nameHash = CommonTasks.getAllNames(kb);
			System.out.println("SIZE: "+nameHash.size());
			Iterator keyIter = nameHash.keySet().iterator();

			// read line
			while (iter.hasNext()) {
				Iterator<String> iter2 = (iter.next()).iterator();
				// read strings
				String term = FileUtils.cleanString(iter2.next());
				//String short_term = FileUtils.cleanString(iter2.next());
				// using the id, let's see if that exists.
				
				 
				//7-8 mj - new comparison to see if the "term" exists within obsolete radlex term.
	            if (term != null && obsoleteTerms.contains(term.toLowerCase().trim()))
	 	        {
	 	         	System.out.println("Obsolete FOUND |"+ term+"|"+nameHash.get(term.replace("_", " ").toLowerCase()));
	 	        }
	            
	            if( term != null && nameHash.containsKey(term.toLowerCase()) ) 
	            {
	            	System.out.println("FOUND|"+term+"|"+nameHash.get(term.toLowerCase()));
	            } 
	            else if (term != null && nameHash.containsKey(term.replace("_", " ").toLowerCase()) ) 
	            {
	            	System.out.println("FOUND w/ _|"+term+"|"+nameHash.get(term.replace("_", " ").toLowerCase()));
	            } 
	            else if (term != null && synHash.containsKey(term.toLowerCase())) 
	            {
	            	System.out.println("FOUND SYN|"+term+"|"+synHash.get(term.toLowerCase()));
	            } 
	            else if (term != null && synHash.containsKey(term.replace("_", " ").toLowerCase())) 
	            {
	            	System.out.println("FOUND SYN w/ _|"+term+"|"+synHash.get(term.replace("_", " ").toLowerCase()));
	            	
	            }
	            else 
	            {
	            	System.out.println("NOFOUND|"+term);
	            }
	             
			} 

			CommonTasks.displayErrors(errors);
		}
		
}

